package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import dicts.PaymentDict;
import dicts.RealmDict;
import entities.TBm;
import entities.TMerchant;
import interfaces.IBmService;
import interfaces.IMerchantService;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class BmAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IMerchantService merchantService;
	private IBmService bmService;

	private TMerchant merchantBean;
	private List<TMerchant> merchantList;
	private TBm bmBean;
	private List<TBm> bmList;

	private String bmNo;
	private String bmNos;
	private String merNo;
	private String bmType;
	private Double bmAmount;
	private String bmState;
	private Date bmDate;
	private Date bmDateTop;
	private Date bmDateEnd;
	private String bmVstate;
	private Date bmVdate;
	private Date bmVdateTop;
	private Date bmVdateEnd;
	private String bmRemark;

	// 展示对象
	public String show() throws Exception {
		if (!bmService.exist(bmNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			bmBean = bmService.fetch(bmNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String editdeposit() throws Exception {
		return SUCCESS;
	}

	public String savedeposit() throws Exception {
		JSONObject resultMap = new JSONObject();
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("merNo", merNo);
		paramsMap.put("bmAmount", NumberUtil.transfer(bmAmount));
		paramsMap.put("bmRemark", bmRemark);
		resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Deposit", paramsMap));
		addActionError("返回码[" + resultMap.getString("code") + "]返回信息[" + resultMap.getString("message") + "]");
		return SUCCESS;
	}

	public String listdeposit() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bmNo", StringUtil.split(bmNo));
		paramMap.put("merNo", merNo);
		paramMap.put("bmType", PaymentDict.BM_DEPOSIT);
		paramMap.put("bmAmount", bmAmount);
		paramMap.put("bmState", bmState);
		paramMap.put("bmDate", ParameterUtil.dateCombin(bmDateTop, bmDateEnd));
		paramMap.put("bmVstate", bmVstate);
		paramMap.put("bmVdate", ParameterUtil.dateCombin(bmVdateTop, bmVdateEnd));
		paramMap.put("bmRemark", bmRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bmDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBm> resultSet = bmService.search(querySet);
		bmList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String exportdeposit() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bmNo", StringUtil.split(bmNo));
		paramMap.put("merNo", merNo);
		paramMap.put("bmType", PaymentDict.BM_DEPOSIT);
		paramMap.put("bmAmount", bmAmount);
		paramMap.put("bmState", bmState);
		paramMap.put("bmDate", ParameterUtil.dateCombin(bmDateTop, bmDateEnd));
		paramMap.put("bmVstate", bmVstate);
		paramMap.put("bmVdate", ParameterUtil.dateCombin(bmVdateTop, bmVdateEnd));
		paramMap.put("bmRemark", bmRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bmDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBm> resultSet = bmService.search(querySet);
		bmList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("商户编号");
		fieldHead.add("预存类型");
		fieldHead.add("金额");
		fieldHead.add("预存状态");
		fieldHead.add("发生日期日期");
		fieldHead.add("发生日期时间");
		fieldHead.add("审核状态");
		fieldHead.add("审核日期日期");
		fieldHead.add("审核日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TBm bmBean : bmList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(bmBean.getBmNo());
			fieldItem.add(bmBean.getMerNo());
			fieldItem.add(bmBean.getBmType());
			fieldItem.add(bmBean.getBmAmount().toString());
			fieldItem.add((bmBean.getBmState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(bmBean.getBmDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(bmBean.getBmDate(), DateUtil.TIME));
			fieldItem.add((bmBean.getBmVstate().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(bmBean.getBmVdate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(bmBean.getBmVdate(), DateUtil.TIME));
			fieldItem.add(bmBean.getBmRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IBmService getBmService() {
		return bmService;
	}

	public void setBmService(IBmService bmService) {
		this.bmService = bmService;
	}

	public TBm getBmBean() {
		return bmBean;
	}

	public void setBmBean(TBm bmBean) {
		this.bmBean = bmBean;
	}

	public List<TBm> getBmList() {
		return bmList;
	}

	public void setBmList(List<TBm> bmList) {
		this.bmList = bmList;
	}

	public String getBmNo() {
		return bmNo;
	}

	public void setBmNo(String bmNo) {
		this.bmNo = bmNo;
	}

	public String getBmNos() {
		return bmNos;
	}

	public void setBmNos(String bmNos) {
		this.bmNos = bmNos;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getBmType() {
		return bmType;
	}

	public void setBmType(String bmType) {
		this.bmType = bmType;
	}

	public Double getBmAmount() {
		return bmAmount;
	}

	public void setBmAmount(Double bmAmount) {
		this.bmAmount = bmAmount;
	}

	public String getBmState() {
		return bmState;
	}

	public void setBmState(String bmState) {
		this.bmState = bmState;
	}

	public Date getBmDate() {
		return bmDate;
	}

	public void setBmDate(Date bmDate) {
		this.bmDate = bmDate;
	}

	public Date getBmDateTop() {
		return bmDateTop;
	}

	public void setBmDateTop(Date bmDateTop) {
		this.bmDateTop = bmDateTop;
	}

	public Date getBmDateEnd() {
		return bmDateEnd;
	}

	public void setBmDateEnd(Date bmDateEnd) {
		this.bmDateEnd = bmDateEnd;
	}

	public String getBmVstate() {
		return bmVstate;
	}

	public void setBmVstate(String bmVstate) {
		this.bmVstate = bmVstate;
	}

	public Date getBmVdate() {
		return bmVdate;
	}

	public void setBmVdate(Date bmVdate) {
		this.bmVdate = bmVdate;
	}

	public Date getBmVdateTop() {
		return bmVdateTop;
	}

	public void setBmVdateTop(Date bmVdateTop) {
		this.bmVdateTop = bmVdateTop;
	}

	public Date getBmVdateEnd() {
		return bmVdateEnd;
	}

	public void setBmVdateEnd(Date bmVdateEnd) {
		this.bmVdateEnd = bmVdateEnd;
	}

	public String getBmRemark() {
		return bmRemark;
	}

	public void setBmRemark(String bmRemark) {
		this.bmRemark = bmRemark;
	}

	public IMerchantService getMerchantService() {
		return merchantService;
	}

	public void setMerchantService(IMerchantService merchantService) {
		this.merchantService = merchantService;
	}

	public TMerchant getMerchantBean() {
		return merchantBean;
	}

	public void setMerchantBean(TMerchant merchantBean) {
		this.merchantBean = merchantBean;
	}

	public List<TMerchant> getMerchantList() {
		return merchantList;
	}

	public void setMerchantList(List<TMerchant> merchantList) {
		this.merchantList = merchantList;
	}

}
